#include<stdio.h>
long long b,n=0,total=0;
int arr[1001]={0};
void input(){
    FILE *fp=fopen("NR01.INP","r");
    fscanf(fp,"%lld%lld",&n,&b);
    fclose(fp);
}
void getNext(){
    int i=0;
    while(arr[i]==1)i++;
    if(i>total)total=i;
    arr[i]=1;
    while(i>=0)arr[--i]=0;
}
long long ketqua(){
    long long s=0;
    for(int i=total;i>=0;i--)s=b*s+arr[i];
    return s;
} 
void process(){
    FILE *fp=fopen("NR01.OUT","w");
    fprintf(fp,"0");
    long long s=1;
    do{
        getNext();
        s=ketqua();
        if(s>n)break;
        fprintf(fp,"\n%lld",s);
    }while(1);
    fclose(fp);
}
int main(){
    input();
    process();
    return 0;
}   
